package org.example.cardgame.utils;

import lombok.Data;
import org.example.cardgame.entity.Card;
import org.example.cardgame.entity.CardGroup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Data
public class MaxCardComputer {

    private List<Card> pokers;        //自己可用的所有牌

    private CardGroup maxGroup;        //组成最大牌型的五张牌

    private List<Card> pokerBackList;


    /**
     *
     * @param holdPokers
     * @param publicPokers
     */
    public MaxCardComputer(List<Card> holdPokers, List<Card> publicPokers) {
        pokers = new ArrayList<>();
        pokerBackList = new ArrayList<>();
        if (holdPokers != null) {
            pokers.addAll(holdPokers);
            pokerBackList.addAll(holdPokers);
        }
        if (publicPokers != null) {
            pokers.addAll(publicPokers);
            pokerBackList.addAll(publicPokers);
        }
        computeMaxCardGroup();
    }

    private void computeMaxCardGroup() {
        if (pokers.size() == 5) {
            CardGroup group = new CardGroup(pokers);
            maxGroup = group;
        } else if (pokers.size() == 6) {
            List<CardGroup> groups = new ArrayList <>();
            for (int i = 0; i < pokerBackList.size(); i++) {
                List<Card> pokerList = new ArrayList<>();
                for (int j = 0; j < pokerBackList.size(); j++) {
                    if (i != j) {
                        pokerList.add(pokerBackList.get(j));
                    }
                }
                groups.add(new CardGroup(pokerList));
            }
            Collections.sort(groups, new CardGroupComparator());
            maxGroup = groups.get(0);
        } else if (pokers.size() == 7) {
            List<CardGroup> groups = new ArrayList <>();
            for (int i = 0; i < 7; i++) {
                for (int j = 0; j < 7; j++) {
                    if (i == j) {
                        continue;
                    }
                    List<Card> pokerList = new ArrayList<>();
                    for (int k = 0; k< pokerBackList.size(); k++) {
                        if (i != k && j != k) {
                            pokerList.add(pokerBackList.get(k));
                        }
                    }
                    groups.add(new CardGroup(pokerList));
                }
            }
            Collections.sort(groups, new CardGroupComparator());
            maxGroup = groups.get(0);
        }
    }
}
